import Vue from 'vue'
import Router from 'vue-router'

// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

// 动态路由表 => 动态路由(需要权限才可以访问的)
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]

Vue.use(Router)

/* Layout */
// layout没必要懒加载 因为大部分打开都要看到layout
import Layout from '@/layout'

// 静态路由表
export const constantRoutes = [
  // 登录页
  {
    path: '/login',
    // 动态加载（路由懒加载）只要匹配到了相对于的路径，才加载改组件
    component: () => import('@/views/login/index'),
    hidden: true
  },

  // 404规则
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // 默认路径,首页
  {
    path: '/',

    component: Layout,
    redirect: '/dashboard',
    // 首页的二级路由
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }), // 组件切换 回到顶部
  // 配置路由规则
  routes: [
    ...constantRoutes,
    ...asyncRoutes
  ]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
